{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "3xW4O0zuKfWD"
      },
      "outputs": [],
      "source": [
        "import os\n",
        "from openai import OpenAI\n",
        "from google.colab import userdata"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "api_key = userdata.get('OPENAI_API_KEY')\n",
        "MODEL = \"gpt-4o-mini\"\n",
        "\n",
        "openai = OpenAI(api_key=api_key)"
      ],
      "metadata": {
        "id": "vUydnLeLKs03"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Chat Completion API\n",
        "\n",
        "https://platform.openai.com/docs/guides/conversation-state?api-mode=chat"
      ],
      "metadata": {
        "id": "5wlqKfVBKyjD"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Using this array for adding all the messages in the conversation\n",
        "history = [\n",
        "    { \"role\": \"user\", \"content\": \"Tell me a math joke\"},\n",
        "]\n",
        "\n",
        "response = openai.chat.completions.create(\n",
        "    model=MODEL,\n",
        "    messages= history\n",
        ")\n",
        "print(response.choices[0].message.content)"
      ],
      "metadata": {
        "id": "dBrUkMCUKz0s",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "9a53705c-ae60-44c7-b8fe-6cc55f044f26"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Why was the equal sign so humble?\n",
            "\n",
            "Because it knew it wasn't less than or greater than anyone else!\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "history.append(response.choices[0].message) # Adding last response from Chat API to history\n",
        "history.append({ \"role\": \"user\", \"content\": \"Explain it to me\"})\n",
        "response2 = openai.chat.completions.create(\n",
        "    model=MODEL,\n",
        "    messages= history\n",
        ")\n",
        "print(response2.choices[0].message.content)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "WItchxDLK2x6",
        "outputId": "28465176-1aab-4739-b348-b41353338a14"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Sure! The joke plays on the concept of an equal sign, which is represented by \"=\", meaning that two values or expressions have the same amount or are identical. \n",
            "\n",
            "When the joke says the equal sign is \"humble,\" it suggests that it doesn't see itself as better or worse than other mathematical symbols, like the less-than sign (\"<\") or the greater-than sign (\">\"). \n",
            "\n",
            "In a way, it anthropomorphizes the equal sign, giving it human traits: being humble or modest because it just states that two things are equal without making any comparisons or claims of superiority. The humor comes from this playful way of assigning personality to a mathematical symbol!\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Responses API\n",
        "\n",
        "https://platform.openai.com/docs/guides/conversation-state?api-mode=responses"
      ],
      "metadata": {
        "id": "tCzSosP5K-JR"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "By default `store` property is set to true, so previous conversation is stored and we can provide reference to previous conversation"
      ],
      "metadata": {
        "id": "gBzxAbx8XQKK"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "response = openai.responses.create(\n",
        "    model=MODEL,\n",
        "    input=\"Tell me a math joke\",\n",
        ")\n",
        "print(\"Response 1 id = \",response.id)\n",
        "print(\"Response 1 = \",response.output_text)\n",
        "print(\"--------------------\")\n",
        "\n",
        "# Does not have any clue about the previous conversation\n",
        "# By default store property is true but we are not sending previous response id\n",
        "response = openai.responses.create(\n",
        "    model=MODEL,\n",
        "    input=\"Explain it to me\",\n",
        ")\n",
        "print(\"Response 2 id = \",response.id)\n",
        "print(\"Response 2 = \", response.output_text)"
      ],
      "metadata": {
        "id": "2IPLSF30K--S",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "795ee8ec-d999-4d05-8e3e-32ef3baeeb38"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Response 1 id =  resp_67e1448a04008192bc733bbd191b4d3c0458cadba34eca25\n",
            "Response 1 =  Why was the equal sign so humble? \n",
            "\n",
            "Because it knew it wasn’t less than or greater than anyone else!\n",
            "--------------------\n",
            "Response 2 id =  resp_67e1448aff9481928349a4d9d92dfe7c011ed9d41b3e4002\n",
            "Response 2 =  Of course! What would you like me to explain? Please provide a specific topic or question, and I'll do my best to help.\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "##### Implementing in the same way as the Chat API"
      ],
      "metadata": {
        "id": "z1gX_OTVXTrB"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "history = [\n",
        "    { \"role\": \"user\", \"content\": \"Tell me a math joke\"},\n",
        "]\n",
        "response = openai.responses.create(\n",
        "    model=MODEL,\n",
        "    input=history, # Sending array of messages\n",
        ")\n",
        "print(\"Response 1 id = \",response.id)\n",
        "print(\"Response 1 = \",response.output_text)\n",
        "print(\"--------------------\")\n",
        "\n",
        "history.append(response.output[0]) # Adding only output property from last response from Chat API to history\n",
        "history.append({ \"role\": \"user\", \"content\": \"Explain it to me\"})\n",
        "response = openai.responses.create(\n",
        "    model=MODEL,\n",
        "    input=history,\n",
        ")\n",
        "print(\"Response 2 id = \",response.id)\n",
        "print(\"Response 2 = \", response.output_text)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "IZd8AWE0XX0q",
        "outputId": "89f30046-2ab8-4a08-9758-05712424f2ea"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Response 1 id =  resp_67e14494f92c8192b2456f009264d1ee0788f9691a720dc1\n",
            "Response 1 =  Why was six afraid of seven?\n",
            "\n",
            "Because seven eight (ate) nine!\n",
            "--------------------\n",
            "Response 2 id =  resp_67e1449662fc81929275dcd5a80f4f0f0788f9691a720dc1\n",
            "Response 2 =  Sure! The joke plays on the word \"eight,\" which sounds like \"ate.\" \n",
            "\n",
            "Here's the breakdown:\n",
            "\n",
            "- The phrase \"seven eight nine\" sounds like a sequence of numbers (7, 8, 9).\n",
            "- However, when you say it out loud, it can also be interpreted as \"seven ate nine,\" implying that seven literally consumed (or \"ate\") nine.\n",
            "\n",
            "The humor comes from the absurdity of numbers having the ability to eat each other and the play on words. So, six is \"afraid\" because it thinks seven might eat it next!\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "##### Using a new conversation state by sending the previous response id"
      ],
      "metadata": {
        "id": "gP22hrfVXdal"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "response = openai.responses.create(\n",
        "    model=MODEL,\n",
        "    input=\"Tell me a math joke\",\n",
        ")\n",
        "print(\"Response 1 id = \",response.id)\n",
        "print(\"Response 1 = \",response.output_text)\n",
        "print(\"--------------------\")\n",
        "\n",
        "# Sending previous response id\n",
        "response = openai.responses.create(\n",
        "    model=MODEL,\n",
        "    previous_response_id=response.id,\n",
        "    input=\"Explain it to me\",\n",
        ")\n",
        "print(\"Response 2 id = \",response.id)\n",
        "print(\"Response 2 = \", response.output_text)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "ksudE5ikXeLy",
        "outputId": "4411fbaa-eff0-4a10-9d4d-864ea4cf71cd"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Response 1 id =  resp_67e1449fbb908192b184093da90690e90d4a0ff694be8674\n",
            "Response 1 =  Why was the equal sign so humble?\n",
            "\n",
            "Because it knew it wasn't less than or greater than anyone else!\n",
            "--------------------\n",
            "Response 2 id =  resp_67e144a0fa148192bccc064b9d7bf5800d4a0ff694be8674\n",
            "Response 2 =  Sure! The joke plays on the concept of the equal sign (=) in mathematics, which signifies that two expressions are the same or equal to each other. \n",
            "\n",
            "When it says the equal sign is \"humble,\" it suggests that it doesn't think of itself as being \"greater than\" or \"less than\" any other number or expression. In the context of math, these terms refer to inequality (greater than > or less than <), while the equal sign denotes equality. \n",
            "\n",
            "So, the humor comes from imagining the equal sign having feelings and personality, and being modest about its role in math!\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "##### Will generate error if we mark `store` property false and use previous response id"
      ],
      "metadata": {
        "id": "gGn2oridYqzp"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "response = openai.responses.create(\n",
        "    model=MODEL,\n",
        "    input=\"Tell me a math joke\",\n",
        "    store=False # Not storing the response\n",
        ")\n",
        "print(\"Response 1 id = \",response.id)\n",
        "print(\"Response 1 = \",response.output_text)\n",
        "\n",
        "print(\"--------------------\")\n",
        "\n",
        "# Generating error saying previous response id not found\n",
        "response = openai.responses.create(\n",
        "    model=MODEL,\n",
        "    previous_response_id=response.id,\n",
        "    input=\"Explain it to me\",\n",
        ")\n",
        "print(\"Response 2 id = \",response.id)\n",
        "print(\"Response 2 = \", response.output_text)"
      ],
      "metadata": {
        "id": "e7G2Jh4KX5Rp"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}